package com.yuyou.train.lesson.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.train.lesson.service.ILessonClassService;
import com.yuyou.train.lesson.vo.req.ReqClassAdd;
import com.yuyou.train.lesson.vo.req.ReqClassUpdateBase;
import com.yuyou.train.lesson.vo.req.ReqClassUpdateDetail;
import com.yuyou.train.service.api.admin.service.AuthUserAPI;
import com.yuyou.train.service.api.lesson.dto.LessonClassDetailDTO;
import com.yuyou.train.service.api.lesson.dto.LessonClassPageDTO;
import com.yuyou.train.service.api.lesson.entity.LessonClass;
import com.yuyou.train.util.bean.LoginRedisObj;
import com.yuyou.train.util.bean.PageParams;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.bean.ReturnResult;
import com.yuyou.train.util.enums.user.UserEnums;
import com.yuyou.train.util.tool.DefindBeanUtils;

@Validated
@RestController
@RequestMapping(value="/api/class")
public class LessonClassController extends BaseController{

	
	@Autowired
	private ILessonClassService lessonClassService;
	
	@Autowired
	private AuthUserAPI authUserAPI;
	
	@ApiOperation(value="新增班级")
	@RequestMapping(value="/addClass",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<LessonClass>> addClass(HttpServletRequest request,@Validated @ModelAttribute ReqClassAdd reqAddClass){
		LoginRedisObj obj=this.getUserObj(request);
		ResBoolSimpleInfo<LessonClass> res=new ResBoolSimpleInfo<LessonClass>();
		LessonClass _class=new LessonClass();
		DefindBeanUtils.copyProperties(reqAddClass, _class);
		res=lessonClassService.addClass(obj.getUserId(),_class, reqAddClass.getUserIds());		
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value="启动或禁用班级")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "班级ID", paramType="query", required = true),
		@ApiImplicitParam(name = "isUse", value = "是否启用：0：否，1：是", paramType="query", required = true)
	})
	@RequestMapping(value="/operateClass",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> operateClass(
			HttpServletRequest request,
			@NotBlank (message="id不能为空")String id,
			@NotBlank (message="isUse不能为空")String isUse
			){	
		LoginRedisObj obj=this.getUserObj(request);
		ResBoolSimpleInfo<String> res=lessonClassService.operateClass(obj.getUserId(),id,isUse);
		return ReturnResult.success(res);
	}
	
	
	@ApiOperation(value="更新班级基本资料")
	@RequestMapping(value="/updateBaseClass",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<LessonClass>> updateBaseClass(HttpServletRequest request,@Validated @ModelAttribute ReqClassUpdateBase reqClassUpdateBase){	
		LoginRedisObj obj=this.getUserObj(request);
		ResBoolSimpleInfo<LessonClass> res=new ResBoolSimpleInfo<LessonClass>();
		LessonClass _class=new LessonClass();
		DefindBeanUtils.copyProperties(reqClassUpdateBase, _class);
		res=lessonClassService.updateClass(obj.getUserId(),_class, reqClassUpdateBase.getUserIds());
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value="更新班级详情")
	@RequestMapping(value="/updateDetailClass",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<LessonClass>> updateDetailClass(HttpServletRequest request,@Validated @ModelAttribute ReqClassUpdateDetail reqClassUpdateDetail){	
		LoginRedisObj obj=this.getUserObj(request);
		ResBoolSimpleInfo<LessonClass> res=new ResBoolSimpleInfo<LessonClass>();
		LessonClass _class = new LessonClass();
		DefindBeanUtils.copyProperties(reqClassUpdateDetail, _class);
		res=lessonClassService.updateClass(obj.getUserId(),_class, null);
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value="删除班级")
	@ApiImplicitParam(name = "id", value = "班级ID", paramType="query", required = true)
	@RequestMapping(value="/delClass",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<String>> delClass(
			HttpServletRequest request,
			 @NotBlank (message="班级ID(id)不能为空")String id){	
		LoginRedisObj obj=this.getUserObj(request);
		ResBoolSimpleInfo<String> res=lessonClassService.delLessonClass(obj.getUserId(),id);
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value="班级详情")
	@RequestMapping(value="/detailClass",method = RequestMethod.POST)
	public ReturnResult<LessonClassDetailDTO> detailClass(HttpServletRequest request,@RequestParam(value = "id")@NotBlank (message="班级ID(id)不能为空") String id){
		LoginRedisObj obj=this.getUserObj(request);
		LessonClassDetailDTO res = lessonClassService.selectDetail(obj.getUserId(),id,UserEnums.type.teacher.getValue());
		return ReturnResult.success(res);
	}
		
	@ApiOperation(value="班级分页")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "name", value = "名字", paramType="query", required = false),
		@ApiImplicitParam(name = "areaId", value = "区域ID", paramType="query", required = false),
		@ApiImplicitParam(name = "isUse", value = "状态：是否启用：0：禁用，1：启用", paramType="query", required = false)
	})
	@RequestMapping(value="/findClassPage",method=RequestMethod.POST)
	public ReturnResult<Pagination<LessonClassPageDTO>> findClassPage(
			HttpServletRequest request,
			@Validated @ModelAttribute PageParams pageParams,
			String name,
			String areaId,
			String isUse){

		Pagination<LessonClassPageDTO> queryPgn = new Pagination<LessonClassPageDTO>();	
		LoginRedisObj obj=this.getUserObj(request);		
		List<String> userAreaIds=authUserAPI.findUseAreaIdListById(obj.getUserId());
		if(CollectionUtils.isEmpty(userAreaIds) ){
			userAreaIds.add("");
		}	
		Map<String, Object> conditions=new HashMap<String, Object>();
		conditions.put("name", name);
		conditions.put("areaId", areaId);
		conditions.put("isUse", isUse);
		conditions.put("userAreaIds", userAreaIds.toArray());
		queryPgn.setConditions(conditions);
		queryPgn.setPageParams(pageParams);
		int total=lessonClassService.findLessonClassPageCount(queryPgn);
		queryPgn.setTotal(total);
		queryPgn.toBePage();
		if(total>0){
			queryPgn.setTotal(total);
			List<LessonClassPageDTO> list=lessonClassService.findLessonClassPageList(queryPgn);	
			queryPgn.setList(list);
		}	
		return ReturnResult.success(queryPgn);
	}


	@ApiOperation(value="所有班级列表")
	@RequestMapping(value="/findClassSelect", method=RequestMethod.POST)
	public ReturnResult<List<LessonClass>> findClassSelect(HttpServletRequest request){
		LoginRedisObj obj=getUserObj(request);	
		
		List<LessonClass> lessClassList =null;
		String loginUserId=obj.getUserId();
		//数据权限
		if(obj.getTag().equals(LoginRedisObj.tagEnum.portal.getValue()) 
				&& obj.getType().equals(UserEnums.type.teacher.getValue())){
			lessClassList=lessonClassService.findLessonClassByUserId(loginUserId);
		}else{
			lessClassList = lessonClassService.findClass();
		}
		return ReturnResult.success(lessClassList);
	}
	
}
